//
//  MSDatePickerView.m
//  JYG_Sales
//
//  Created by apple on 15/10/29.
//  Copyright © 2015年 WeShare. All rights reserved.
//

#import "MSDatePickerView.h"

@interface MSDatePickerView ()
@property(nonatomic,strong)UIView *backgroundView;
@property(nonatomic,strong)UIDatePicker *datePicker;
@end
@implementation MSDatePickerView

- (id)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, APPFRAME.size.width, 290);
        self.backgroundColor = [UIColor whiteColor];
        [self initUI];
    }
    return self;
}

- (void)initUI
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, APPFRAME.size.width, 40)];
    titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    titleLabel.textColor = [UIColor blackColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"选择时间";
    [self addSubview:titleLabel];
    
    self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, titleLabel.bottom, APPFRAME.size.width, self.height-titleLabel.height-60)];
    self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
    [self.datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh_Hans_CN"]];
    [self.datePicker setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"]];
    self.datePicker.maximumDate = [NSDate date];
    [self addSubview:self.datePicker];
    
    CGFloat btnWidth = (APPFRAME.size.width-50)/2.0;
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelBtn.frame = CGRectMake(15, self.datePicker.bottom, btnWidth, 40);
    cancelBtn.layer.cornerRadius = 5;
    cancelBtn.layer.borderColor = [UIColor lightGrayColor].CGColor;
    cancelBtn.layer.borderWidth = 1;
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [cancelBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelBtn];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    confirmBtn.frame = CGRectMake(APPFRAME.size.width-15-btnWidth, self.datePicker.bottom, btnWidth, 40);
    confirmBtn.layer.cornerRadius = 5;
    confirmBtn.layer.borderColor = kBlueColor.CGColor;
    confirmBtn.layer.borderWidth = 1;
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:kBlueColor forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont fontWithName:@"STHeitiSC-Light" size:15];
    [confirmBtn addTarget:self action:@selector(confirmAction) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:confirmBtn];
    
    self.height = cancelBtn.bottom+20;
}

- (void)show
{
    if (!self.backgroundView) {
        self.backgroundView = [[UIView alloc] initWithFrame:SCREENFRAME];
        self.backgroundView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.4f];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        tapGesture.delegate = (id)self;
        [self.backgroundView addGestureRecognizer:tapGesture];
        [self.backgroundView addSubview:self];
    }
    if (self.backgroundView.superview) {
        [self.backgroundView removeFromSuperview];
    }
    if (!self.backgroundView.superview) {
        UIWindow *window = [UIApplication sharedApplication].windows[0];
        [window addSubview:self.backgroundView];
    }
    self.top = self.backgroundView.bottom;
    [UIView animateWithDuration:0.3 animations:^{
        self.top = self.backgroundView.bottom-self.height;
    }];
}

- (void)confirmAction
{
    if ([self.datePicker.date compare:[NSDate date]]==NSOrderedDescending) {
        return;
    }
    self.pickBlock(self.datePicker.date);
    [self dismiss];
}

- (void)tapAction
{
    [self dismiss];
}

- (void)dismiss
{
    [self.backgroundView removeFromSuperview];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer*)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint touchPoint = [touch locationInView:self.backgroundView];
    return !CGRectContainsPoint(self.frame, touchPoint);
}

@end
